package de.tum.in.eist.im.client;

import com.google.gwt.user.client.ui.RootPanel;

import de.tum.in.eist.im.client.view.IView;

/**
 * The {@link ClientView} class holds all information about the view and
 * represents the data stored in the {@link ClientModel} class.
 * 
 * @author Christian Ivicevic (ga73yis@mytum.de)
 * @since 08-31-2011
 * @version 08-31-2011
 */
public class ClientView {
	/**
	 * Private pointer to the {@link ClientModel} object used to retrieve data
	 * from. (currently not used, but for extensibility not yet removed.)
	 */
	private ClientModel currentModel = null;
	/**
	 * Holds the current {@link IView} object.
	 */
	private IView currentView = null;
	/**
	 * Boolean value indicating whether the view should NOT be rendered.
	 */
	private boolean isClosing = false;

	/**
	 * Initializes a new instance of the {@link ClientView} class with the
	 * specified {@link ClientModel} container.
	 * 
	 * @param currentModel
	 *            the {@link ClientModel} container to use.
	 */
	public ClientView(ClientModel currentModel) {
		this.currentModel = currentModel;
	}
	
	/**
	 * Renders the specified {@link IView} object.
	 * @param view the {@link IView} object to render.
	 */
	public void render() {
		if(this.isClosing)
			return;
		this.currentView.update();
	}
	
	/**
	 * Renders the specified {@link IView} object.
	 * @param view the {@link IView} object to render.
	 */
	public void render(IView view) {
		if(this.isClosing)
			return;
		RootPanel.get().clear();
		this.currentView = view.create(this.currentModel);
		this.currentView.render();
	}
	
	/**
	 * Locks the {@code render()} function.
	 */
	public void requestLock() {
		this.isClosing = true;
	}
}
